package com.rtos.vo.memory;

import com.rtos.model.process.PCB;
import javafx.application.Platform;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @Author: biubiu0002
 * @Date: 2021-10-11 19:40
 **/
public class MemoryRectangle extends Pane {
    private final Color[] colors = new Color[]{Color.AQUA, Color.BURLYWOOD, Color.CORNFLOWERBLUE, Color.RED, Color.YELLOW, Color.YELLOWGREEN, Color.GREEN};
    private Rectangle rectangle;
//    private ProgressIndicator pi;
    private Label pidLabel;

    public MemoryRectangle(double start,double Y, double width, double height, int pid) {
        this.setLayoutX(start);
        this.setLayoutY(Y);


        this.rectangle = new Rectangle(width, height);
        this.rectangle.setX(0);
        this.rectangle.setY(0);
//        this.rectangle.setLayoutX(start);
//        this.rectangle.setLayoutY(Y);
        this.rectangle.setWidth(width);
        this.rectangle.setHeight(height);
        this.rectangle.setStroke(Color.BLACK);
        this.rectangle.setFill(this.getRandomColor());
        this.rectangle.setArcWidth(5.0D);
        this.rectangle.setArcHeight(5.0D);
        this.getChildren().add(this.rectangle);
//        this.pi = new ProgressIndicator();
//        this.pi.setLayoutY(10.0D);
//        this.pi.setPrefSize(width, width);
//        this.pi.setProgress(0.0D);

//        Thread rectangleThread = new Thread(() -> {
//            while (pcb.getRestRunTime() != 0.0D) {
//                Platform.runLater(() -> {
//                    MemoryRectangle.this.pi.setProgress((pcb.getTotalRunTime() - pcb.getRestRunTime()) / pcb.getTotalRunTime());
//                });
//
//                try {
//                    Thread.sleep(10L);
//                } catch (InterruptedException var2) {
//                    Logger.getLogger(MemoryRectangle.class.getName()).log(Level.SEVERE, (String) null, var2);
//                }
//            }
//
//        }, "rectangleThread "+ UUID.randomUUID().toString().substring(0,4));
//        rectangleThread.setDaemon(true);
//        rectangleThread.start();

//        //todo 系统进程
//        if (pcb.getPID() != -1) {
//            this.getChildren().add(this.pi);
//        }

        this.pidLabel = new Label("#"+String.valueOf(pid));
        this.pidLabel.setLayoutY(100.0D);
        this.pidLabel.setLayoutX(width / 2.0D - 10.0D);
        this.getChildren().add(this.pidLabel);
    }

    public MemoryRectangle(double start, double width, double height, int pid) {
        this.rectangle = new Rectangle(width, height);
        this.setLayoutX(start);

        this.rectangle.setStroke(Color.BLACK);
        this.rectangle.setFill(this.getRandomColor());
        this.rectangle.setArcWidth(15.0D);
        this.rectangle.setArcHeight(15.0D);
        this.getChildren().add(this.rectangle);
//        this.pi = new ProgressIndicator();
//        this.pi.setLayoutY(10.0D);
//        this.pi.setPrefSize(width, width);
//        this.pi.setProgress(0.0D);

//        Thread rectangleThread = new Thread(() -> {
//            while (pcb.getRestRunTime() != 0.0D) {
//                Platform.runLater(() -> {
//                    MemoryRectangle.this.pi.setProgress((pcb.getTotalRunTime() - pcb.getRestRunTime()) / pcb.getTotalRunTime());
//                });
//
//                try {
//                    Thread.sleep(10L);
//                } catch (InterruptedException var2) {
//                    Logger.getLogger(MemoryRectangle.class.getName()).log(Level.SEVERE, (String) null, var2);
//                }
//            }
//
//        }, "rectangleThread "+ UUID.randomUUID().toString().substring(0,4));
//        rectangleThread.setDaemon(true);
//        rectangleThread.start();

//        //todo 系统进程
//        if (pcb.getPID() != -1) {
//            this.getChildren().add(this.pi);
//        }

        this.pidLabel = new Label(String.valueOf(pid));
        this.pidLabel.setLayoutY(100.0D);
        this.pidLabel.setLayoutX(width / 2.0D - 10.0D);
        this.getChildren().add(this.pidLabel);
    }

    private Color getRandomColor() {
        return this.colors[(int) (Math.random() * (double) this.colors.length)];
    }

}
